package blackjack;

import java.util.Random;
import java.util.Scanner;

/**
 * Created by musing on 1/6/14.
 */
public class Blackjack {

    private static String userInput;
    private static Scanner scanner = new Scanner(System.in);

    public Blackjack(){

    }

    public static void main(String[] args){

        Random random = new Random();
        Player player = new Player();
        Player dealer = new Player();

        player.draw(random);
        player.draw(random);

        dealer.draw(random);
        dealer.draw(random);

        out("Welcome to blackjack!");
        out("You get a " + player.getCard(0) + " and a " + player.getCard(1) + ".");
        out("Your total is " + player.getTotal());

        out("\n");
        out("The dealer has a " +  dealer.getCard(0) + " showing, and a hidden card.");
        out("His total is hidden, too.");

        Scanner scanner = new Scanner(System.in);

        //player's turn
        while(!player.busts()){
            out("\n");
            out("Would you like to \"hit\" or \"stay\"?");
            validateInput();
            if(userInput.equals("hit")){
                out("You drew a " + player.draw(random));
                out("Your total is " + player.getTotal());

            }else{
                break;
            }
        }
        if(player.busts()){
            out("You busted!");
            out("The dealer wins!");
            return;
        }
        //dealers turn
        out("Okay, dealer's turn.");
        out("His hidden card was a " + dealer.getCard(0));
        out("His total was " + dealer.getTotal());

        while(dealer.getTotal() <= 16){
            out("\n");
            out("Dealer chooses to hit.");
            out("He draws a " + dealer.draw(random) + ".");
            out("His total is " + dealer.getTotal() + ".");
            out("\n");

        }
        if(dealer.busts()){
            out("Dealer busted!");
            out("You win!");
            return;
        }

        out("Dealer total is " + dealer.getTotal() + ".");
        out("Your total is " + player.getTotal() + ".");

        if(player.getTotal() > dealer.getTotal()){
            out("You WIN!");
        }else{
            out("You Lose!");
        }





    }

    private static void out(String output){
        System.out.println(output);
    }

    private static void validateInput(){
        userInput = scanner.nextLine();
        while(((!userInput.equals("hit")) && (!userInput.equals("stay")))){

            out("Please choose either \"hit\" or \"stay\".");
            out("Would you like to \"hit\" or \"stay\"?");
            validateInput();
        }


    }

    private static void busting(Scanner scanner, String userInput){

    }
}
